package com.maolong.springbootstartordercenter.config;


import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Queue;

import java.util.HashMap;

@Configuration
@Slf4j
@EnableRabbit
@Data
@ConfigurationProperties("spring.rabbitmq")
public class RabbitMqConfigPlus {
    /**
     * 1.开启RabbitMq
     * 2.配置队列
     * 3.配置交换机
     * 4.绑定队列和交换机
     * 5.配置消息转换器
     * 6.配置消息确认和回调
     * */
    private String host;
    private int port;
    private String username;
    private String password;


    private static final String EXCHANGE_NAME="spring-plus-exchange";
    private static final String QUEUE_NAME="spring-plus-queue1";
    private static final String QUEUE_NAME2="spring-plus-queue2";

    /**
     * 建立连接工厂
     * @return
     */
    @Bean
    CachingConnectionFactory connectionFactory(){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        System.out.println(host);
        System.out.println(port);
        System.out.println(username);
        System.out.println(password);
        return connectionFactory;
    }


    /**
     * 手动创建rabbitTemplate实例
     * @param ackCallBack
     * @param returnCallBack
     * @return
     */
    @Bean
    RabbitTemplate rabbitTemplate(AckCallBack ackCallBack,ReturnCallBack returnCallBack){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
        rabbitTemplate.setConfirmCallback(ackCallBack);
        rabbitTemplate.setReturnCallback(returnCallBack);
        rabbitTemplate.setMandatory(true);//true交换机回退 false 直接丢弃
        return rabbitTemplate;
    }

    /**
     * 创建测试队列
     * @return
     */
    @Bean
    public Queue queue(){
        return new Queue(QUEUE_NAME, true);
    }

    /**
     * 创建测试交换机
     * CustomExchange是自定义交换机
     * TopicExchange
     * DirectExchange
     * FanoutExchange
     * HeadersExchange
     *需要安装延时队列的插件！！！
     */
    @Bean
    public CustomExchange exchange(){
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("x-delayed-type","direct");
//        CustomExchange customExchange = new CustomExchange(EXCHANGE_NAME,"x-delayed-message",true,false,map);
        CustomExchange customExchange = new CustomExchange(EXCHANGE_NAME,"topic",true,false);
        return customExchange;
    }

    @Bean
    public Binding queueBinding(){
        return BindingBuilder.bind(queue()).to(exchange()).with("spring.plus").noargs();
    }

}
